''// pages/toPayfor/toPayfor.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    soiId: '',
    arrowImage: '/images/order_arrow.png',
    wxImage: '/images/wxImage.png',
    shbOrderInfoDtoModel: [],
    // 倒计时
    timer: '',
  },

  // 加载数据
  loadData(solid) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'orderInfo/queryOrderInfoTime?',
      data: {
        soiId: solid,
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.messcode == '001') {
          that.setData({
            shbOrderInfoDtoModel: res.data.ShbOrderInfoDto,
          })
          that.countdown(res.data.ShbOrderInfoDto.time, 60 * 30, "0:0", 0)
        } else if (res.data.messcode == '002') {
          // 没有数据
        } else {
          // 服务器异常
        }
      },
    })
  },
  // 调用微信支付
  payClick: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.login({
      success: function(res) {
        if (res.code) {
          console.log(res.data)
          wx.request({
            url:  'https://www.shanhaibian.com/shb_website_web/wxPay/wxWatAliPay',
            data: {
              code: res.code,
              state: that.data.soiId,
            },
            method: 'GET',
            header: {
              'Content-Type': 'application/json'
            },
            success(res) {
              console.log(res.data)
              wx.hideLoading()
              if (res.data.messcode == '001') {
                wx.requestPayment({
                  timeStamp: res.data.finalpackage.timeStamp,
                  nonceStr: res.data.finalpackage.nonceStr,
                  package: res.data.finalpackage.package,
                  signType: res.data.finalpackage.signType,
                  paySign: res.data.finalpackage.paySign,
                  'success': function (res) {
                    wx.navigateTo({
                      url: '../paySuccess/paySuccess?soiId=' + that.data.shbOrderInfoDtoModel.soiId + '&actualPrice=' + that.data.shbOrderInfoDtoModel.actualPrice,
                    })
                  },
                  'fail': function (res) {
                    console.log(res)
                  }
                })
              } else if (res.data.messcode == '002') {
                // 没有数据
              } else {
                // 服务器异常
              }
            },
          }) 
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.loadData(options.soiId)
    that.setData({
      soiId: options.soiId
    })
  },
  // 倒计时
  countdown(endTime, totalTime, timeType, timeFlag) {
    var that = this;
    var EndTime = endTime || [];
    var NowTime = Date.parse(new Date()) / 1000;
    var total_micro_second = EndTime + totalTime - NowTime || [];
    console.log('剩余时间：' + total_micro_second);
    var cutdownT = that.dateformat(total_micro_second, timeFlag)
    console.log(cutdownT)
    // 渲染倒计时时钟
    that.setData({
      finishT: cutdownT
    });
    if (total_micro_second <= 0) {
      that.setData({
        finishT: timeType
      });
      return;
    }
    that.setData({
      timer: setTimeout(function () {
        total_micro_second -= 1;
        that.countdown(endTime, totalTime, timeType, timeFlag);
      }
        , 1000)
    })
  },

  // 时间格式化输出，如11:03 25:19 每1s都会调用一次
  dateformat(micro_second, timeFlag) {
    // 总秒数
    var second = Math.floor(micro_second);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    // 分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    if (timeFlag == 1) {
      return hr + ":" + min + ":" + sec;
    } else if (timeFlag == 2) {
      return day + "天" + hr + ":" + min + ":" + sec;
    } else {
      return min + ":" + sec;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})